﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
import flash.external.ExternalInterface;
class Polygon{
	static private var aColor:Array;
	static private var textFormat:TextFormat;
	static private var defaultLink:String;
	static private var asyncMode:Boolean=false;
	static private var tipTF:TextField;
	private var aX:Array;
	private var aY:Array;
	private var id:Number;
	private var type:Number=0;
	private var name:String="";
	private var link:String="";
	private var highlight:Boolean=false;
	private var outStand:Boolean=false;
	private var outStandMC:MovieClip;
	private var graphicMC:MovieClip;
	public function Polygon(node:XMLNode){
		if(aColor==undefined){
			aColor=new Array();
			aColor.push(0x75cdff);// 建筑
			aColor.push(0xa1c6e8);// 河域
			aColor.push(0x78a411);// 绿地
			aColor.push(0xe0d887);// 区域(黄)
			aColor.push(0xb598d0);// 区域(紫)
			aColor.push(0xd4a2a5);// 区域(红)
			aColor.push(0xbacaad);// 区域(绿)
		}
		
		if(textFormat==undefined){
			textFormat=new TextFormat();
			textFormat.size=12;
		}
		
		if (node.nodeName!="PG") {
			throw new Error("Not A Polygon Node!");
		}
		
		var idValue=parseInt(node.attributes["ID"]);
		if(!isNaN(idValue))
			id=idValue;
		
		// 设置Point数据
		var aPoint:Array = new Array();
		for (var child:XMLNode = node.firstChild; child != null; child = child
				.nextSibling) {
			var nodeName:String = child.nodeName;
			if (nodeName=="Name") {
				this.name=child.firstChild.nodeValue;
			} else if (nodeName=="Link") {
				this.link=child.firstChild.nodeValue;
			} else if (nodeName=="Type") {
				this.type=parseInt(child.firstChild.nodeValue);
			} else if (nodeName=="P") {
				var x:Number = undefined;
				var y:Number = undefined;
				for (var pc:XMLNode = child.firstChild; pc != null; pc = pc
						.nextSibling) {
					if (pc.nodeName=="X")
						x = parseInt(pc.firstChild.nodeValue);
					else if (pc.nodeName=="Y")
						y = parseInt(pc.firstChild.nodeValue);
				}
				if (x == undefined || y == undefined) {
					throw new Error("Polygon Node Point Err!");
				}
				aPoint.push(new Point(x, y));
			}
		}
		aX=new Array();
		aY=new Array();
		for (var i:Number = 0; i < aPoint.length; i++) {
			var p:Point = aPoint[i];
			aX[i] = p.x;
			aY[i] = p.y;
		}
		//计算链接URL
		if(defaultLink!=undefined&&link==undefined){
			link=defaultLink;
			var pIndex:Number=defaultLink.indexOf("[Polygon]");
			if(pIndex!=-1){
				var aSplit=link.split("[Polygon]");
				link="";
				for(var j:Number=0;j<aSplit.length-1;j++){
					link+=aSplit[j]+name;
				}
				link+=aSplit[aSplit.length-1];
			}
			var iIndex:Number=defaultLink.indexOf("[ID]");
			if(iIndex!=-1){
				var aSplit=link.split("[ID]");
				link="";
				for(var j:Number=0;j<aSplit.length-1;j++){
					link+=aSplit[j]+id;
				}
				link+=aSplit[aSplit.length-1];
			}
		}
	}
	
	static public function setDefaultLink(theLink:String):Void{
		defaultLink=theLink;
	}
	
	static public function setTipTF(theTipTF:TextField):Void{
		tipTF=theTipTF;
	}
	
	public function drawGraphicPart(mc:MovieClip):Void{
		graphicMC=mc;
		mc.lineStyle(0,aColor[type]);
		mc.beginFill(aColor[type],100);
		mc.moveTo(aX[0],aY[0]);
		for(var i:Number=0;i<aX.length-1;i++){
			mc.lineTo(aX[i+1],aY[i+1]);
		}
		mc.lineTo(aX[0],aY[0]);
		mc.endFill();
		//添加事件处理
		mc.tipTF=tipTF;
		var highlightMC:MovieClip;
		mc.highlightMC=highlightMC;
		mc.aX=aX;
		mc.aY=aY;
		var fontSize:Object=textFormat.getTextExtent(name);
		mc.fontSize=fontSize;
		var polygon:Polygon=this;
		mc.polygon=polygon;
		mc.onRollOver = function():Void{
			var name:String=polygon.getName();
			if(name!=undefined&&name!=""){
				tipTF.text=name;
				tipTF._width=fontSize.textFieldWidth;
				tipTF._visible=true;
			}
			if(highlightMC!=undefined)
			{
				highlightMC.removeMovieClip();
				highlightMC=undefined;
			}
			highlightMC=this._parent.createEmptyMovieClip("highlightMC", this._parent.getNextHighestDepth());
			highlightMC.lineStyle(1,0xffcc33,100,true,"none");
			highlightMC.moveTo(aX[0],aY[0]);
			for(var i:Number=0;i<aX.length-1;i++){
				highlightMC.lineTo(aX[i+1],aY[i+1]);
			}
			highlightMC.lineTo(aX[0],aY[0]);
		};
		mc.onRollOut = function():Void{
			tipTF.text="";
			tipTF._visible=false;
			if(highlightMC!=undefined)
			{
				highlightMC.removeMovieClip();
				highlightMC=undefined;
			}
		};
		mc.onRelease = function():Void{
			if(Polygon.isAsyncMode()){
				var id:Number=polygon.getID();
				var name:String=polygon.getName();
				ExternalInterface.call("onPolygonClicked",name,id);
			}
			else{
				var link:String=polygon.getLink();
				if(link!=undefined&&link!=""){
					getUrl(link,"_blank");
				}
			}
		};
	}
	public function setHighlighted(highlighted:Boolean):Void{
		this.highlight=highlighted;
	}
	public function isHighlighted():Boolean{
		return highlight;
	}
	public function getLink():String{
		return link;
	}
	public function getName():String{
		return name;
	}
	
		public function getCenterX():Number{
		var maxX = Number.MIN_VALUE;
		var minX = Number.MAX_VALUE;
		for (var i:Number = 0; i < aX.length; i++) {
			if (aX[i] > maxX)
				maxX = aX[i];
			if (aX[i] < minX)
				minX = aX[i];
		}
		return (minX+maxX)/2;
	}
	public function getCenterY():Number{
		var maxY = Number.MIN_VALUE;
		var minY = Number.MAX_VALUE;
		for (var i:Number = 0; i < aY.length; i++) {
			if (aY[i] > maxY)
				maxY = aY[i];
			if (aY[i] < minY)
				minY = aY[i];
		}
		return (minY+maxY)/2;
	}
	
	static public function isAsyncMode():Boolean{
		return asyncMode;
	}
	
	static public function setAsyncMode(async:Boolean):Void{
		asyncMode=async;
	}
	
	public function getID():Number{
		return id;
	}
	
	public function setOutStand(outStand:Boolean):Void{
		if(this.outStand!=outStand){
			if(outStand){
				outStandMC=graphicMC._parent.createEmptyMovieClip("outStandMC", graphicMC._parent.getNextHighestDepth());
				outStandMC.lineStyle(1,0xfd7c5e,100,true,"none");
				outStandMC.moveTo(aX[0],aY[0]);
				for(var i:Number=0;i<aX.length-1;i++){
					outStandMC.lineTo(aX[i+1],aY[i+1]);
				}
				outStandMC.lineTo(aX[0],aY[0]);
			}else{
				outStandMC.removeMovieClip();
			}
			this.outStand=outStand;
		}
	}
	
	public function isOutStand():Boolean{
		return outStand;
	}
	
}